package com.kiln.juxingqs.dialog

import android.view.View
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.viewModelScope
import client.global.commlib.base.PagerFooterAdapter
import client.global.commlib.network.data.PagingJsonResult
import client.global.commlib.toast.toast
import client.global.commlib.widget.EmptyLayout
import com.kiln.juxingqs.R
import com.kiln.juxingqs.adapter.GetPagingBathRetAdapter
import com.kiln.juxingqs.api.bean.GetBathRet
import com.kiln.juxingqs.databinding.LayoutPagingbathdialogBinding
import com.kiln.juxingqs.ui.viewmodel.MiddleVm
import com.tzstore.oo.dialog.BottomBindingDialog
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import java.util.*

open class PagingZhBathDialog(private val context: FragmentActivity) :
    BottomBindingDialog<LayoutPagingbathdialogBinding>(context) {

    var okTxt: String = "确定"
    var cancleTxt: String = "取消"
    var ok: ((b: GetBathRet.Bath) -> Unit)? = null
    var isLoadMore = false
    lateinit var vm: MiddleVm

    val adapter: GetPagingBathRetAdapter by lazy {
        GetPagingBathRetAdapter().apply {
            ok = {
                this@PagingZhBathDialog.ok?.invoke(
                    it
                )
                cancel()
            }
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
            appendError = {
                isLoadMore = true
            }
        }
    }

    override fun getV(): Int {
        return R.layout.layout_pagingbathdialog
    }

    //挂卖获取批次
    fun holdBath() {
        vm.viewModelScope.launch {
            vm.pagingDataAuto(20, hold = {
                val ret = vm.server.freeze_locking_deal(it).body()
                PagingJsonResult(
                    list = ret?.data?.bath_list,
                    code = ret?.code,
                    message = ret?.message
                )
            }).collect {
                adapter.submitData(it)
            }
        }
    }

    override fun postBinding() {
        binding.event = this
        setCanceledOnTouchOutside(true)
        binding.recyclerView.apply {
            adapter = this@PagingZhBathDialog.adapter.withLoadStateFooter(
                PagerFooterAdapter()
            )
            onLoadMore = {
                if (isLoadMore)
                    this@PagingZhBathDialog.adapter.retry()
                isLoadMore = false
            }
        }
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }

        holdBath()
    }
}